<script setup lang="ts">
import { uploadImage } from '@/services/consult'
import { useStore } from '@/stores'
import type { PartialConsult, Image } from '@/types/consult'
import { showConfirmDialog, showToast } from 'vant'
import { timeOptions, consultOptions } from '@/services/constants'
import type {
  UploaderAfterRead,
  UploaderFileListItem
} from 'vant/lib/uploader/types'
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
const { consult } = useStore()
const router = useRouter()

const illnessForm = ref<PartialConsult>({
  illnessDesc: '',
  illnessTime: undefined,
  consultFlag: undefined,
  pictures: []
})

const pics = ref<Image[]>([])

// 上传业务
const afterRead: UploaderAfterRead = async (obj) => {
  // 1. 多选判断
  if (Array.isArray(obj)) return
  // 2. 非空判断
  if (!obj.file) return
  // 开启上传loading
  obj.status = 'uploading'
  // 提示文字
  obj.message = '上传中...'
  try {
    // 2. 调用上传接口
    const res = await uploadImage(obj.file)
    // 3. 追加提交的pictures
    illnessForm.value.pictures?.push(res.data)
    obj.status = 'done'
    obj.message = ''
    // 借助 obj.url属性 存储id
    obj.url = res.data.id
  } catch {
    obj.status = 'failed'
    obj.message = '上传失败'
  }
}
// 移除图片
const del = (obj: UploaderFileListItem) => {
  // console.log(obj.url) // 记录的那个id 这个id可以去illnessForm.pictures数组中比较
  illnessForm.value.pictures = illnessForm.value.pictures?.filter(
    (item) => item.id !== obj.url
  )
}

// 计算属性
const disabled = computed(() => {
  return (
    !illnessForm.value.illnessDesc?.trim() ||
    illnessForm.value.illnessTime === undefined ||
    illnessForm.value.consultFlag === undefined
  )
})
const next = () => {
  // 1. 校验三个字段
  if (!illnessForm.value.illnessDesc?.trim()) return showToast('请输入病情描述')
  if (illnessForm.value.illnessTime === undefined)
    return showToast('请选择患病时长')
  if (illnessForm.value.consultFlag === undefined)
    return showToast('请选择是否就诊过')
  // 2. 存储信息
  consult.setIllnessForm(illnessForm.value)
  // 3. 跳转到选择患者页面
  router.push('/user/patient?isChange=1')
}

// 回显
const showHistory = () => {
  // 1. 先判断是否有病情描述页面的数据存储
  if (!consult.consult.illnessDesc) return
  // 2. 展示确认弹框
  showConfirmDialog({
    title: '温馨提示',
    message: '是否恢复您之前填写的病情信息呢？',
    closeOnPopstate: false // 取消 回退自动关闭
  })
    .then(() => {
      // 3. 点击确认回显数据
      const { illnessDesc, illnessTime, consultFlag, pictures } =
        consult.consult
      illnessForm.value = { illnessDesc, illnessTime, consultFlag, pictures }
      // 4. 单独将pictures再赋值给 upload组件的pics
      pics.value = pictures || []
    })
    .catch(() => {})
}
showHistory()
</script>

<template>
  <div class="consult-illness-page">
    <cp-nav-bar title="图文问诊" />
    <!-- 医生提示 -->
    <div class="illness-tip van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <div class="info">
        <p class="tit">在线医生</p>
        <p class="tip">
          请描述你的疾病或症状、是否用药、就诊经历，需要我听过什么样的帮助
        </p>
        <p class="safe">
          <cp-icon name="consult-safe" /><span>内容仅医生可见</span>
        </p>
      </div>
    </div>
    <!-- 收集信息 -->
    <div class="illness-form">
      <van-field
        type="textarea"
        rows="3"
        placeholder="请详细描述您的病情，病情描述不能为空"
        v-model="illnessForm.illnessDesc"
      ></van-field>
      <div class="item">
        <p>本次患病多久了？</p>
        <cp-radio-btn
          :options="timeOptions"
          v-model="illnessForm.illnessTime"
        ></cp-radio-btn>
      </div>
      <div class="item">
        <p>此次病情是否去医院就诊过？</p>
        <cp-radio-btn
          :options="consultOptions"
          v-model="illnessForm.consultFlag"
        ></cp-radio-btn>
      </div>
      <!-- 上传图片 -->
      <div class="illness-img">
        <van-uploader
          upload-text="上传图片"
          upload-icon="photo-o"
          max-count="9"
          max-size="5 * 1024 * 1024"
          v-model="pics"
          :after-read="afterRead"
          @delete="del"
        ></van-uploader>
        <p class="tip" v-if="!pics.length">
          上传内容仅医生可见,最多9张图,最大5MB
        </p>
      </div>
      {{ disabled }}
      <!-- 下一步按钮  -->
      <van-button
        :class="{ disabled: disabled }"
        @click="next"
        type="primary"
        block
        round
        >下一步</van-button
      >
    </div>
  </div>
</template>

<style lang="scss" scoped>
.van-button {
  font-size: 16px;
  margin-bottom: 30px;
  &.disabled {
    opacity: 1;
    background: #fafafa;
    color: #d9dbde;
    border: #fafafa;
  }
}

.illness-img {
  padding-top: 16px;
  margin-bottom: 40px;
  display: flex;
  align-items: center;
  .tip {
    font-size: 12px;
    color: var(--cp-tip);
  }
  ::v-deep() {
    .van-uploader {
      &__preview {
        &-delete {
          left: -6px;
          top: -6px;
          border-radius: 50%;
          background-color: var(--cp-primary);
          width: 20px;
          height: 20px;
          &-icon {
            transform: scale(0.9) translate(-22%, 22%);
          }
        }
        &-image {
          border-radius: 8px;
          overflow: hidden;
        }
      }
      &__upload {
        border-radius: 8px;
      }
      &__upload-icon {
        color: var(--cp-text3);
      }
    }
  }
}

.consult-illness-page {
  padding-top: 46px;
}
.illness-tip {
  display: flex;
  padding: 15px;
  .img {
    width: 52px;
    height: 52px;
    border-radius: 4px;
    overflow: hidden;
    margin-top: 10px;
  }
  .info {
    flex: 1;
    padding-left: 12px;
    .tit {
      font-size: 16px;
      margin-bottom: 5px;
    }
    .tip {
      padding: 12px;
      background: var(--cp-bg);
      color: var(--cp-text3);
      font-size: 13px;
      margin-bottom: 10px;
    }
    .safe {
      font-size: 10px;
      color: var(--cp-text3);
      display: flex;
      align-items: center;
      .cp-icon {
        font-size: 12px;
        margin-right: 2px;
      }
    }
  }
}
.illness-form {
  padding: 15px;
  .van-field {
    padding: 0;
    &::after {
      border-bottom: none;
    }
  }
  .item {
    > p {
      color: var(--cp-text3);
      padding: 15px 0;
    }
  }
}
</style>
